package com.sy.simpleegg.mvp.model

import com.sy.simpleegg.base.BaseModel
import com.sy.simpleegg.bean.SupplementOrderBean
import com.sy.simpleegg.constant.PayType
import com.sy.simpleegg.ext.logFile
import com.sy.simpleegg.mvp.contract.SupplementContract
import com.sy.simpleegg.room.RoomUtil

class SupplementModel : BaseModel(), SupplementContract.Model {
    override suspend fun getOrder(
        startDate: String,
        endDate: String,
    ): MutableList<SupplementOrderBean> {
        return try {
            val supplementOrderList = mutableListOf<SupplementOrderBean>()
            RoomUtil.orderSupplement(startDate, endDate)?.run {
                forEach { orderData ->
                    val isSupplementOrder = when (orderData.payType) {
                        PayType.QRCODE -> true
                        PayType.CASH -> true
                        PayType.CARD -> true
                        PayType.WITHDRAWAL -> true
                        PayType.PAIBI -> false
                        PayType.CLEAR -> false
                        PayType.WRITEOFF -> true
                    }
                    if (!isSupplementOrder) return@forEach
                    supplementOrderList.add(
                        SupplementOrderBean(
                            orderData.id,
//                            "${orderData.date.replace("-", "")}${orderData.time.replace(":", "")}",
                            "${orderData.date} ${orderData.time}",
                            orderData.money.ifEmpty { "0" },
                            orderData.amount.ifEmpty { "0" },
                            orderData.amount.ifEmpty { "0" } == orderData.realAmount.ifEmpty { "0" },
                            orderData.realAmount.ifEmpty { "0" }
                        )
                    )
                }
            }
            supplementOrderList
        } catch (e: Exception) {
            e.printStackTrace()
            logFile("获取出币异常订单异常：${e.message}")
            mutableListOf<SupplementOrderBean>()
        }
    }

    override suspend fun setRealAmount(id: Long, realAmount: String) {
        try {
            RoomUtil.setOrderRealAmount(id, realAmount)
        }catch (e: Exception){
            e.printStackTrace()
            logFile("更新出币异常数据异常：${e.message}")
        }
    }

}